# Copyright 2022 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_python//python:proto.bzl", "py_proto_library")
load("//pw_build:pigweed.bzl", "pw_cc_test")
load("//pw_protobuf_compiler:pw_proto_library.bzl", "pw_proto_library")

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

hosts_lin_mac = {
    "@platforms//os:linux": [],
    "@platforms//os:macos": [],
    "//conditions:default": ["@platforms//:incompatible"],
}

cc_library(
    name = "config",
    hdrs = ["public/pw_transfer/internal/config.h"],
    includes = ["public"],
    deps = [
        ":config_override",
        "//pw_chrono:system_clock",
    ],
)

label_flag(
    name = "config_override",
    build_setting_default = "//pw_build:default_module_config",
)

cc_library(
    name = "core",
    srcs = [
        "chunk.cc",
        "client_context.cc",
        "context.cc",
        "public/pw_transfer/internal/chunk.h",
        "public/pw_transfer/internal/client_context.h",
        "public/pw_transfer/internal/context.h",
        "public/pw_transfer/internal/event.h",
        "public/pw_transfer/internal/protocol.h",
        "public/pw_transfer/internal/server_context.h",
        "rate_estimate.cc",
        "server_context.cc",
        "transfer_thread.cc",
    ],
    hdrs = [
        "public/pw_transfer/handler.h",
        "public/pw_transfer/rate_estimate.h",
        "public/pw_transfer/transfer_thread.h",
    ],
    includes = ["public"],
    deps = [
        ":config",
        ":transfer_pwpb.pwpb",
        ":transfer_pwpb.raw_rpc",
        "//pw_bytes",
        "//pw_chrono:system_clock",
        "//pw_containers:intrusive_list",
        "//pw_log",
        "//pw_log:rate_limited",
        "//pw_preprocessor",
        "//pw_protobuf",
        "//pw_result",
        "//pw_rpc:client_server",
        "//pw_rpc:internal_packet_cc.pwpb",
        "//pw_rpc/raw:client_api",
        "//pw_rpc/raw:server_api",
        "//pw_status",
        "//pw_stream",
        "//pw_sync:binary_semaphore",
        "//pw_sync:timed_thread_notification",
        "//pw_thread:thread_core",
        "//pw_varint",
    ],
)

cc_library(
    name = "pw_transfer",
    srcs = [
        "transfer.cc",
    ],
    hdrs = [
        "public/pw_transfer/transfer.h",
    ],
    includes = ["public"],
    deps = [
        ":core",
        "//pw_assert",
        "//pw_bytes",
        "//pw_log",
        "//pw_result",
        "//pw_rpc:internal_packet_cc.pwpb",
        "//pw_rpc/raw:server_api",
        "//pw_status",
        "//pw_stream",
    ],
)

cc_library(
    name = "client",
    srcs = [
        "client.cc",
    ],
    hdrs = [
        "public/pw_transfer/client.h",
    ],
    includes = ["public"],
    deps = [
        ":core",
        "//pw_assert",
        "//pw_function",
        "//pw_log",
        "//pw_stream",
    ],
)

cc_library(
    name = "atomic_file_transfer_handler",
    srcs = ["atomic_file_transfer_handler.cc"],
    hdrs = [
        "public/pw_transfer/atomic_file_transfer_handler.h",
    ],
    includes = ["public"],
    deps = [
        ":atomic_file_transfer_handler_internal",
        ":core",
        "//pw_log",
        "//pw_stream:std_file_stream",
    ],
)

cc_library(
    name = "atomic_file_transfer_handler_internal",
    srcs = [
        "pw_transfer_private/filename_generator.h",
    ],
)

cc_library(
    name = "test_helpers",
    srcs = [
        "pw_transfer_private/chunk_testing.h",
    ],
    deps = [
        ":core",
        "//pw_containers",
    ],
)

pw_cc_test(
    name = "chunk_test",
    srcs = ["chunk_test.cc"],
    deps = [
        ":core",
        "//pw_unit_test",
    ],
)

pw_cc_test(
    name = "handler_test",
    srcs = ["handler_test.cc"],
    # TODO: b/235345886 - Fix transfer tests on Windows and non-host builds.
    target_compatible_with = select(hosts_lin_mac),
    deps = [
        ":pw_transfer",
        "//pw_unit_test",
    ],
)

pw_cc_test(
    name = "atomic_file_transfer_handler_test",
    srcs = ["atomic_file_transfer_handler_test.cc"],
    # TODO: b/235345886 - Fix transfer tests on Windows and non-host builds.
    target_compatible_with = select(hosts_lin_mac),
    deps = [
        ":atomic_file_transfer_handler",
        ":atomic_file_transfer_handler_internal",
        ":pw_transfer",
        "//pw_random",
        "//pw_string",
        "//pw_unit_test",
    ],
)

pw_cc_test(
    name = "transfer_thread_test",
    srcs = ["transfer_thread_test.cc"],
    # TODO: b/235345886 - Fix transfer tests on Windows and non-host builds.
    target_compatible_with = select(hosts_lin_mac),
    deps = [
        ":pw_transfer",
        ":test_helpers",
        "//pw_rpc:test_helpers",
        "//pw_rpc/raw:client_testing",
        "//pw_rpc/raw:test_method_context",
        "//pw_thread:thread",
        "//pw_unit_test",
    ],
)

pw_cc_test(
    name = "transfer_test",
    srcs = ["transfer_test.cc"],
    # TODO: b/235345886 - Fix transfer tests on Windows and non-host builds.
    target_compatible_with = select(hosts_lin_mac),
    deps = [
        ":pw_transfer",
        ":test_helpers",
        "//pw_assert",
        "//pw_containers",
        "//pw_rpc:test_helpers",
        "//pw_rpc/raw:test_method_context",
        "//pw_thread:thread",
        "//pw_unit_test",
    ],
)

pw_cc_test(
    name = "client_test",
    srcs = ["client_test.cc"],
    # TODO: b/235345886 - Fix transfer tests on Windows and non-host builds.
    target_compatible_with = select(hosts_lin_mac),
    deps = [
        ":client",
        ":test_helpers",
        "//pw_rpc:test_helpers",
        "//pw_rpc/raw:client_testing",
        "//pw_thread:sleep",
        "//pw_thread:thread",
        "//pw_unit_test",
    ],
)

proto_library(
    name = "transfer_proto",
    srcs = [
        "transfer.proto",
    ],
)

pw_proto_library(
    name = "transfer_pwpb",
    deps = [":transfer_proto"],
)

py_proto_library(
    name = "transfer_proto_pb2",
    deps = [":transfer_proto"],
)

java_proto_library(
    name = "transfer_proto_java",
    deps = [":transfer_proto"],
)

java_lite_proto_library(
    name = "transfer_proto_java_lite",
    deps = [":transfer_proto"],
)
